using System;
using System.Collections;
using System.Xml;

namespace MediaWikiEngine.Domain
{
	/// <summary>
	/// Summary description for Command.
	/// </summary>
	public abstract class BaseCommand
	{
		protected CommandParams _params = null;
		protected WikiConnection _conn;

		public BaseCommand(CommandParams parameters, WikiConnection connection)
		{
			_params = parameters;
			_conn = connection;
		}

        public BaseCommand(WikiConnection connection)
        {
            _conn = connection;
        }

		public abstract CommandResult Execute();

		/// <summary>
		/// Creates a command with no parameters, using an Xml text writer.
		/// </summary>
		/// <param name="inXW"></param>
		/// <param name="inCommandName"></param>
		static public void CommandToXml(
			XmlTextWriter inXW,
			string inCommandName)
		{
			inXW.WriteStartElement("command");
			inXW.WriteAttributeString("id", inCommandName);
			inXW.WriteEndElement();	//	/command
		}

		static protected void StartCommandToXml(
			XmlTextWriter inXW,
			string inCommandName)
		{
			inXW.WriteStartElement("command");
			inXW.WriteAttributeString("id", inCommandName);
		}

		static protected void EndCommandToXml(
			XmlTextWriter inXW)
		{
			inXW.WriteEndElement();	//	/command
		}

		static protected void StartConditionsToXml(
			XmlTextWriter inXW)
		{
			inXW.WriteStartElement("conditions");
		}

		static protected void EndConditionsToXml(
			XmlTextWriter inXW)
		{
			inXW.WriteEndElement();	//	/conditions
		}

		static protected void IfConditionTestToXml(
			XmlTextWriter inXW,
			string inValue,
			string inRelation,
			string inTest,
			string inFalse)
		{
			inXW.WriteStartElement("if");
			inXW.WriteAttributeString("value", inValue);
			inXW.WriteAttributeString("relation", inRelation);
			inXW.WriteAttributeString("test", inTest);
			inXW.WriteAttributeString("false", inFalse);
			inXW.WriteEndElement();	//	/if
		}

		static protected void IfConditionToXml(
			XmlTextWriter inXW,
			string inValue,
			string inRelation,
			string inFalse)
		{
			inXW.WriteStartElement("if");
			inXW.WriteAttributeString("value", inValue);
			inXW.WriteAttributeString("relation", inRelation);
			inXW.WriteAttributeString("false", inFalse);
			inXW.WriteEndElement();	//	/if
		}

		static protected void StartInputToXml(
			XmlTextWriter inXW)
		{
			inXW.WriteStartElement("input");
		}

		static protected void EndInputToXml(
			XmlTextWriter inXW)
		{
			inXW.WriteEndElement();	//	/input
		}

		static protected void InputNameValueToXml(
			XmlTextWriter inXW,
			string inName,
			string inValue)
		{
			inXW.WriteStartElement("in");
			inXW.WriteAttributeString("name", inName);
			inXW.WriteAttributeString("value", inValue);
			inXW.WriteEndElement();	//	/in
		}

		static protected void StartOutputToXml(
			XmlTextWriter inXW)
		{
			inXW.WriteStartElement("output");
		}

		static protected void EndOutputToXml(
			XmlTextWriter inXW)
		{
			inXW.WriteEndElement();	//	/output
		}

		static protected void OutputNameValueToXml(
			XmlTextWriter inXW,
			string inName,
			string inTarget)
		{
			inXW.WriteStartElement("out");
			inXW.WriteAttributeString("name", inName);
			inXW.WriteAttributeString("target", inTarget);
			inXW.WriteEndElement();	//	/out
		}
	}
}
